/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mvc.example.presentation.controller;

import mvc.example.bussines.domain.Student;
import mvc.example.presentation.events.DataEvent;
import mvc.example.presentation.listeners.StudentSelectedListener;
import mvc.example.presentation.model.AbstractModel;
import mvc.example.presentation.model.StudentsModel;
import mvc.example.presentation.view.StudentsView;

/**
 *
 * @author fernando.hernandez
 */
public class StudentsController extends AbstractController implements StudentSelectedListener
{
    private StudentsModel studentsModel;
    
    public StudentsModel getStudentsModel() {
        return studentsModel;
    }

    public void setStudentsModel(StudentsModel studentsModel) {
        this.studentsModel = studentsModel;
        firePropertyChange("studentsModel", this.studentsModel, studentsModel);
    }

    public StudentsController(StudentsModel studentsModel) {
        setStudentsModel(studentsModel);
        init();
    }

    private void init() {
        StudentsView studentsView = new StudentsView(this);
        studentsView.setVisible(true);
    }
    
    public void selectStudent(Student student){
        studentsModel.setSelectedStudent(student);
    }

    @Override
    protected AbstractModel getModel() {
       return studentsModel;
    }

    public void editStudent() {
       StudentController studentController = new StudentController(getStudentsModel());
       studentController.edit = true;
       
    }

    public void newStudent() {
        getStudentsModel().setSelectedStudent(null);
        StudentController studentController = new StudentController(getStudentsModel());
        studentController.edit = false; 
    }


    @Override
    public void selectedChange(DataEvent e) {
         if(e.getType() == DataEvent.SELECTED_DATA){
            Student student = (Student) e.getSource();
            getStudentsModel().setSelectedStudent(student);
        }
    }


   

  
}
